﻿@model ChatViewModel

<script type="text/javascript">
	$(document).ready(function () {
		setInterval(updateChat, 5000);
		$("#add-chat-message").click(function () {
			updateChat(true);
		});
	});

	function updateChat(newMessage) {
		$.ajax({
			type: "POST",
			url: window.MAIN_WEB_PATH + "Ajax/AddChatMessage",
			dataType: "json",
			data: { id: $("#Id").val(), message: $("#Message").val(), newMessage: newMessage },
			success: function (data, textStatus, jqXHR) {
				if (data != "") {
					$("#live-chat").empty();
					if (newMessage)
						$("#Message").val("");
					var items = JSON.parse(data);
					for (var i = 0; i < items.length; i++) {
						$("#live-chat").append("<li class=\"name\">" + items[i].DateTime + "&nbsp;&nbsp;<a href='" + items[i].Url + "'>" + items[i].Name + "</a></li>");
						$("#live-chat").append("<li class=\"message\">" + items[i].Message + "</li>");
					}
				}
			}
		});
	}
</script>
<div class="chat_container">
<h3>@Html.GetGlobalResource("Chat", "Title")</h3>
<ul id="live-chat" class="chat">
	@foreach (var item in Model.ChatItems) {
		<li class="name">@item.CreateTime.ToString("HH:mm")&nbsp;&nbsp;<a href="@Url.Action("Profile", "Client", new { id = item.Client.UserId })">@item.Client.Login</a></li>
		<li class="message">@item.Message</li>
	}
</ul>
<br/>
@Html.HiddenFor(x => x.Id)
@if (Model.CurrentMembershipUser != null && !Model.readOnly) {
	@Html.TextBox("Message")
	<input id="add-chat-message" type="button" value="@Html.GetGlobalResource("Chat", "Add")"/>
}
</div>